<?php
/**
 * Created by IntelliJ IDEA.
 * User: luwei
 * Date: 2017/9/28
 * Time: 14:11
 */

namespace app\modules\api\models;

use app\utils\GetInfo;
use app\hejiang\ApiResponse;
use app\models\Topic;
use app\models\TopicPl;
use app\models\TopicFavorite;
use yii\helpers\VarDumper;

class TopicPlForm extends ApiModel
{
    public $store_id;
    public $user_id;
    public $topic_id;
    public $comment;

    public function rules()
    {
        return [
            [['store_id','user_id','topic_id'], 'integer'],
			[['comment'], 'required'],
        ];
    }
	public function attributeLabels() 
    { 
        return [ 
            'store_id' => 'Store ID',
            'user_id' => '用户',
			'topic_id'=>'专题id',
			'comment'=>'评论内容',
           
        ]; 
    }
   

     public function save()
    {
		$address = new TopicPl();
        if (!$this->validate()) {
            return $this->errorResponse;
        }

        $address->store_id = $this->store_id;
			
		$model = TopicPl::findOne([
						'user_id' => $this->user_id,
						'topic_id' => $this->topic_id,
						'store_id' => $this->store->id,
					]);
				
					
		if($model){
			    $address->isNewRecord = true; 
        }	
        $address->user_id = $this->user_id;
       
        $address->addtime = time();
			
        $address->topic_id = $this->topic_id;
        $address->comment = $this->comment;

        if ($address->save()) {
            return [
                'code' => 1,
                'msg'  => '评论成功',
            ];
        } else {
            return [
                'code' => 0,
                'msg'  => '操作失败，请稍后重试',
            ];
        }
    }
}
